<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Host Settings</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script src="/js/xkube.js?v=1" charset="utf-8"></script>
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <form class="layui-form layui-form-pane" action="" lay-filter="UpdateInfo">
            <div class="htmlTpl"></div>
            <div class="layui-form-item">
                <div class="layui-input-inline">  
                    <button class="layui-btn layui-btn-normal" id="newaddbtn"><i class="layui-icon layui-icon-add-circle"></i>添加host</button>
                </div> 
            </div> 
            <br>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认提交</button>
                </div>
            </div>
        </form>
    </div>
</div>
</body>
<script type="text/javascript">
    	$(document).ready(function(){
          //GetCurrClusterId();  
          //GetNamespace();	
    	});		
</script>

<script>

    //标签删除
    var TplIndex = 0;
    function delTpl(id) {
      //var TplIndex = id;
      TplIndex--;
      console.log(TplIndex);
      $("#tpl-"+id).remove();
    }

    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

			$.get('/xkube/deploy/v1/Host' + location.search, function (resp) {
  				if (resp.code == 0 ) {
          		$.each(resp.data,function(i,item){
                      TplIndex++;
                      //console.log(TplIndex);
                      var addTpl =
                          '<div class="layui-form-item" id="tpl-'+TplIndex+'">' +
                              '<label class="layui-form-label">域名</label>' +
                              '<div class="layui-input-inline" style="width:200px">' +
                    	              '<input type="text" name="domain[]" id="domain'+TplIndex+'" placeholder="" value="'+item.domain+'" class="layui-input">' +
                              '</div>' +    
                              '<div class="layui-input-inline" style="width:150px">' +  
                                  '<input type="text" name="ip[]" id="ip'+TplIndex+'" placeholder="" value="'+item.ip+'" class="layui-input">' +
                              '</div>' +
                              '<div class="layui-input-inline">' +  
                                  '<input class="layui-btn layui-btn-normal layui-bg-orange layui-icon" style="width:60px" type="button" id="newDelbtn" value="&#xe616;" onclick="delTpl('+TplIndex+');" />' +
                              '</div>' +                
                          '</div>'
                        $('.htmlTpl').append(addTpl);  
          		});    
          	  layui.use('form', function(){
          	           var form = layui.form;
          	           form.render();
          	           //form.render('select', 'clusterId');
          	  });	        
          }else{
            layer.msg('请重新刷新',{icon:2});
          }
		  });

        //labes add
        $('#newaddbtn').on("click",function(){
          TplIndex++;
          //console.log(TplIndex);
          var addTpl =
              '<div class="layui-form-item" id="tpl-'+TplIndex+'">' +
                  '<label class="layui-form-label">域名</label>' +
                  '<div class="layui-input-inline" style="width:200px">' +
        	              '<input type="text" name="domain[]" id="domain'+TplIndex+'" placeholder="域名,多个域名逗号隔开" value="" class="layui-input">' +
                  '</div>' +    
                  '<div class="layui-input-inline" style="width:150px">' +  
                      '<input type="text" name="ip[]" id="ip'+TplIndex+'" placeholder="IP" value="" class="layui-input">' +
                  '</div>' +
                  '<div class="layui-input-inline">' +  
                      '<input class="layui-btn layui-btn-normal layui-bg-orange layui-icon" style="width:60px" type="button" id="newDelbtn" value="&#xe616;" onclick="delTpl('+TplIndex+');" />' +
                  '</div>' +                
              '</div>'
            $('.htmlTpl').append(addTpl);   
            form.render(); 
          return false;   
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
                console.log(data.field);
			          layer.confirm('确定修改?', {icon: 3, title:'提示',yes: function(index){
                     var index2 = layer.load(0, {shade: false});
                     layer.msg('稍等片刻');
                     $.ajax({
                       url: "/xkube/deploy/v1/Host" + location.search,
                       type: "post",
                       data: JSON.stringify(data.field),
                       dataType: "json",
                       success: function (resp) {
                                layer.close(index2);
                                 if(resp.code == 0){
                                    layer.msg('修改成功', {icon: 1});
									                  //window.location.reload();
                                 }else{
                                    layer.msg(resp.msg,{icon:2});
                                 }
                        }
                      });		  	  
                  },
                  cancel: function(index, layero){ 
                    layer.close(index);
                    layer.close(index2);
		                console.log("不操作");
                  } 
                });
              return false;
        });

    });
</script>
</html>